<?php

require_once dirname(__FILE__).'/../lib/articuloGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/articuloGeneratorHelper.class.php';

/**
 * articulo actions.
 *
 * @package    tvpv
 * @subpackage articulo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class articuloActions extends autoArticuloActions
{
	public function processForm(sfWebRequest $request, sfForm $form){
	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $this->getUser()->setFlash('notice', $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.');
      $foto = $form->getValue('foto');
      $this->logMessage("FOTO --->".$request->getParameter('foto'));
      if ( is_object( $foto ) )
      {
       $nombre = sha1( $foto->getOriginalName().$form->getValue('codigo') ).$foto->getExtension();
       //$nombre = sha1($foto . $this->form->getValue('codigo'));     
       $foto->save(sfConfig::get('sf_root_dir').'/web/images/fotos/'.$nombre);
      }
      $Articulo = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $Articulo)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice').' You can add another one below.');

        $this->redirect('@articulo_new');
      }
      else
      {
        $this->redirect('@articulo_edit?id='.$Articulo->getId());
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
    }
	}
	
	  public function addFiltersCriteria($c){
		 if (isset($this->filters['reordenacion']))
     {
      $c->add(ArticuloPeer::EXISTENCIA, ArticuloPeer::EXISTENCIA.'<='.ArticuloPeer::REORDEN, Criteria::CUSTOM);
     }
     return parent::addFiltersCriteria($c);		
	}
}
